﻿using MediatR;
using SheepCultivation.Common.Domain.Purchase_sales;
using SheepCultivation.Common.Domain.PurchaseSales;
using SheepCultivation.Common.Domain.SheepBasicInformation;
using SheepCultivation.Common.Domain.Week2;
using SheepCultivation.Common.Dto;
using SheepCultivation.Common.Infrastructure.Interface;
using SheepCultivation.Enum;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace SheepCultivation.API.AddliCation.PurchaseSales
{
    public class SheepBuySeachHandler : IRequestHandler<SheepBuySeachCommon, Result>
    {
        public readonly IBaseRepository<SheepBuy> SheepBuyRepository;
        public readonly IBaseRepository<Absrtact> AbsrtactRepository;
        public readonly IBaseRepository<Buyer> BuyerRepository;
        public readonly IBaseRepository<Depot> DepotRepository;
        public readonly IBaseRepository<Building> BuildingRepository;
        public readonly IBaseRepository<BasicInformation> BasicInformationRepository;
        public readonly IBaseRepository<Sheepbreed> SheepbreedRepository;
        public readonly IBaseRepository<Vendor> VendorRepository;
        public readonly IBaseRepository<Columns> ColumnsRepository;

        public SheepBuySeachHandler(IBaseRepository<SheepBuy> sheepBuyRepository, IBaseRepository<Absrtact> absrtactRepository,
            IBaseRepository<Buyer> buyerRepository, IBaseRepository<Depot> depotRepository, 
            IBaseRepository<Building> buildingRepository,IBaseRepository<BasicInformation> basicInformationRepository, 
            IBaseRepository<Sheepbreed> sheepbreedRepository, IBaseRepository<Vendor> vendorRepository, 
            IBaseRepository<Columns> columnsRepository)
        {
            SheepBuyRepository = sheepBuyRepository;
            AbsrtactRepository = absrtactRepository;
            BuyerRepository = buyerRepository;
            DepotRepository = depotRepository;
            BuildingRepository = buildingRepository;
            BasicInformationRepository = basicInformationRepository;
            SheepbreedRepository=sheepbreedRepository;
            VendorRepository=vendorRepository;
            ColumnsRepository=columnsRepository;
        }

        public async Task<Result> Handle(SheepBuySeachCommon request, CancellationToken cancellationToken)
        {
            var res = new Result() { Code = (int)CommonEnumCode.error };
            try
            {
                var list = (from a in SheepBuyRepository.GetAll()
                           join b in AbsrtactRepository.GetAll()
                           on a.AbsrtactId equals b.AbsrtactId
                           join c in BuyerRepository.GetAll()
                           on a.BuyerId equals c.BuyerId
                           join d in DepotRepository.GetAll()
                           on a.DepotId equals d.DepotId
                           join e in BuildingRepository.GetAll()
                           on a.BuildingId equals e.BuildingId
                           join f in BasicInformationRepository.GetAll()
                           on a.BreedingSheepArchiveId equals f.BreedingSheepArchiveId
                           join g in SheepbreedRepository.GetAll()
                           on a.SheepTypesId equals g.SheepbreedId
                           join h in VendorRepository.GetAll()
                           on a.VendorId equals h.VendorId
                           join o in ColumnsRepository.GetAll()
                           on a.ColumnsId equals o.ColumnsId
                           select new SheepBuyDTO
                           {
                               
                               SheepBuyId=a.SheepBuyId,
                               Numberes=a.Numberes,
                               BuyDate=a.BuyDate,
                               Sex=a.Sex,
                               Category=a.Category,
                               VendorId=h.VendorId,
                               VendorName=h.VendorName,
                               AbsrtactId=b.AbsrtactId,
                               AbsrtactName=b.AbsrtactName,
                               BreedingSheepArchiveId=f.BreedingSheepArchiveId,
                               BreedingSheepArchiveName=f.Auricle,
                               BuildingId=e.BuildingId,
                               BuildingName=e.BuildingName,
                               ColumnsId=o.ColumnsId,
                               ColumnsName=o.ColumnsName,
                               BuyerId=c.BuyerId,
                               BuyerName=c.BuyerName,
                               DepotId=d.DepotId,
                               DepotName=d.DepotName,
                               GoodName=a.GoodName,
                               Num=a.Num,
                               Price=a.Price,
                               Amount=a.Amount,
                               Settlement =a.Settlement,
                               JPrice= a.Amount-a.Settlement,
                           }).ToList();
                if (!string.IsNullOrEmpty(request.BuyDate))
                {
                    list=list.Where(x=>x.BuyDate>=DateTime.Parse(request.BuyDate)).ToList();
                }
                if (!string.IsNullOrEmpty(request.JBuyDate))
                {
                    list = list.Where(x => x.BuyDate <= DateTime.Parse(request.JBuyDate)).ToList();
                }
                if (request.SheepTypesId > 0)
                {
                    list=list.Where(x=>x.SheepTypesId == request.SheepTypesId).ToList();
                }
                if (request.DepotId > 0)
                {
                    list=list.Where(x=>x.DepotId==request.DepotId).ToList();
                }
                if(request.BuildingId > 0)
                {
                    list = list.Where(x => x.BuildingId == request.BuildingId).ToList();
                }
                if (!string.IsNullOrEmpty(request.BreedingSheepArchiveId))
                {
                    list = list.Where(x => x.BreedingSheepArchiveName == request.BreedingSheepArchiveId).ToList();
                }
                if (request.BuyerId > 0)
                {
                    list = list.Where(x => x.BuyerId == request.BuyerId).ToList();
                }
                if (!string.IsNullOrEmpty(request.Numberes))
                {
                    list = list.Where(x => x.Numberes == request.Numberes).ToList();
                }
                if (request.VendorId > 0)
                {
                    list = list.Where(x => x.VendorId == request.VendorId).ToList();
                }
                res.Code = (int)CommonEnumCode.success;
                res.Data = list;
                res.Msg = "羊只采购显示";
            }
            catch (Exception)
            {

                throw;
            }
            return res;
        }
    }
    public class SheepBuyDTO()
    {
        public int SheepBuyId { get; set; }
        /// <summary>
        /// 耳号
        /// </summary>
        public int BreedingSheepArchiveId { get; set; }
        public string BreedingSheepArchiveName { get; set; }
        /// <summary>
        /// 栋舍
        /// </summary>
        public int BuildingId { get; set; }
        public string BuildingName { get; set; }
        /// <summary>
        /// 转入栏位
        /// </summary>
        public int ColumnsId { get; set; }
        public string ColumnsName { get; set; }
        /// <summary>
        /// 性别
        /// </summary>
        public int Sex { get; set; }
        /// <summary>
        /// 商品名称
        /// </summary>
        public string GoodName { get; set; }
        /// <summary>
        /// 计量单位
        /// </summary>
        public string Unit { get; set; }
        /// <summary>
        /// 入库数量
        /// </summary>
        public int Num { get; set; }
        /// <summary>
        /// 只均单价
        /// </summary>
        public decimal Price { get; set; }
        /// <summary>
        /// 金额
        /// </summary>
        public decimal Amount { get; set; }
        /// <summary>
        /// 单位折扣
        /// </summary>
        public decimal Settlement { get; set; }
        /// <summary>
        /// 总重量
        /// </summary>
        public int Weight { get; set; }
        /// <summary>
        /// 采购员
        /// </summary>
        public int BuyerId { get; set; }
        public string BuyerName { get; set; }
        /// <summary>
        /// 类别
        /// </summary>
        public string Category { get; set; }
        /// <summary>
        /// 摘要
        /// </summary>
        public int AbsrtactId { get; set; }
        public string AbsrtactName { get; set; }
        /// <summary>
        /// 采购日期
        /// </summary>
        public DateTime BuyDate { get; set; }
        /// <summary>
        /// 核算单元Id
        /// </summary>
        public int AccountingunitId { get; set; }
        public string AccountingunitName { get; set; }
        /// <summary>
        /// 单据号
        /// </summary>
        public string? Numberes { get; set; }
        /// <summary>
        /// 羊只类型
        /// </summary>
        public int SheepTypesId { get; set; }
        public string SheepTypesName { get; set; }
        /// <summary>
        /// 供应商
        /// </summary>
        public int VendorId { get; set; }
        public string VendorName { get; set; }
        /// <summary>
        /// 流水号
        /// </summary>
        public string? SerialNumber { get; set; }
        /// <summary>
        /// 仓库
        /// </summary>
        public int DepotId { get; set; }
        public string DepotName{get;set;}
        /// <summary>
        /// 结算金额
        /// </summary>
        public decimal JPrice { get; set; }
    }
}
